// components/blog-ctrl/blog-ctrl.js
let userInfo = {}
let db = wx.cloud.database()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    blogId: String,
    blog: Object,
  },

  externalClasses: ['iconfont', 'icon-iconfontfenxiang', 'icon-pinglun'],

  /**
   * 组件的初始数据
   */
  data: {
    // 登录组件是否显示
    loginShow: false,
    modalShow: false,
    content: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onComments() {
      // 判断用户是否授权
      wx.getSetting({
        success: (res) => {
          if (res.authSetting['scope.userInfo']) {
            wx.getUserInfo({
              success: (res) => {
                userInfo = res.userInfo
                // 显示评论弹出层
                this.setData({
                  modalShow: true,
                })
              }
            })
          } else {
            this.setData({
              loginShow: true,
            })
          }
        }
      })
    },

    onLoginsuccess(event) {
      userInfo = event.detail
      // 授权框消失，评论框显示
      this.setData({
        loginShow: false,
      }, () => {
        this.setData({
          modalShow: true,
        })
      })
    },

    onLoginfail() {
      wx.showModal({
        title: '评论权限需授权',
        content: '',
      });
      // this.onComments()
    },

    // onInput(event){
    //   this.setData({
    //     content: event.detail.value
    //   })
    // },

    onSend(event) {
      // 插入数据库
      // console.log(event)
      // let fromId = event.detail.fromId
      let content = event.detail.value.content
      console.log(content)
      if (content.trim() == '') {
        wx.showModal({
          title: '评论内容不能为空',
          content: '',
        })
        return
      }
      console.log(userInfo)
      wx.showLoading({
        title: '评价中',
        mask: true,
      })
      wx.request({
        url: 'http://127.0.0.1:8000/blog_comment/',
        method: 'POST',
        data: {
          content,
          nickname: userInfo.nickName,
          avatarUrl: userInfo.avatarUrl,
          blogId: this.properties.blogId,
        },
        success: ((res) =>{
          console.log(res)
          wx.hideLoading()
          wx.showToast({
            title: res.data.errmsg,
          })
          // 父元素刷新评论页面
          this.triggerEvent('refreshCommentList')
          this.setData({
            modalShow: false,
            content: '',
          })
        })
      })
       
      // db.collection('blog-comment').add({
      //   data: {
      //     content,
      //     createTime: db.serverDate(),
      //     blogId: this.properties.blogId,
      //     nickName: userInfo.nickName,
      //     avatarUrl: userInfo.avatarUrl
      //   }
      // }).then(() => {

      //   wx.hideLoading()
      //   wx.showToast({
      //     title: '评论成功',
      //   })
      //   this.setData({
      //     modalShow: false,
      //     content: '',
      //   })
        // 父元素刷新评论页面
        this.triggerEvent('refreshCommentList')

      // })

    },

  }
})
